package agents.election;

import java.util.Comparator;

public class StateElectionJudge implements Comparator<StateBallot> {

	private static StateElectionJudge instance = null;
	
	public static StateElectionJudge getInstance() {
		if(instance == null)
			instance = new StateElectionJudge();
		
		return instance;
	}
	
	@Override
	public int compare(StateBallot s1, StateBallot s2) {
		
		if(s1 == null)
			return -1;
		else if(s2 == null)
			return 1;
		
		if(s1.getWinnerCount() != 0 && s2.getWinnerCount() != 0) { // Ambos vencedores
			if(s1.getWinnerCount() < s2.getWinnerCount())
				return 1;
			else if(s1.getWinnerCount() > s2.getWinnerCount())
				return -1;
			else { // DESEMPATE
				if(s1.getWaitTime() > s2.getWaitTime())
					return 1;
				else if(s1.getWaitTime() < s2.getWaitTime())
					return -1;
				else return 0;
			}
		} else {
			if(s1.getWinnerCount() == 0)
				return 1;
			else if(s2.getWinnerCount() == 0)
				return -1;
			else { // (Ambos os votos são não-vencedores)
				if(s1.getVote() > s2.getVote())
					return 1;
				else if(s1.getVote() < s2.getVote())
					return -1;
				else { // DESEMPATE (Quem espera há mais tempo)
					if(s1.getWaitTime() > s2.getWaitTime())
						return 1;
					else if(s1.getWaitTime() < s2.getWaitTime())
						return -1;
					else { // DESEMPATE - PUTZ! (Quem pediu primeiro)
						if(s1.hashCode() < s2.hashCode())
							return 1;
						else if(s1.hashCode() > s2.hashCode())
							return -1;
						// ELSE - são o mesmo voto!!!
					}
				}
			}
		}
		
		return 0;
	}

}
